﻿#region

using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Http;
using Vin.Tool.Domain.ExceptionModel;

#endregion

namespace Vin.Tool.Core.AssertCategory;

public class ValidatorTool
{
    public static bool TryOnlyValidateObject<T>(T obj, bool validateAllProperties = true, bool throwException = true)
    {
        // var (res, _) = TryValidateObject(obj, validateAllProperties, throwException);
        // return res;
        return TryValidateObject(obj, validateAllProperties, throwException).Item1;
    }

    public static (bool, List<ValidationResult>) TryValidateObject<T>(T obj, bool validateAllProperties = true,
        bool throwException = true)
    {
        var validationResults = new List<ValidationResult>();
        var result =
            Validator.TryValidateObject(obj!, new ValidationContext(obj!), validationResults, validateAllProperties);

        if (result) return (true, validationResults);

        if (throwException)
            throw new ErrorException(StatusCodes.Status500InternalServerError, validationResults.First().ErrorMessage!);

        return (result, validationResults);
    }
}